//
//  AddViewController.m
//  DaiShengInvest
//
//  Created by 张睿 on 2017/7/3.
//  Copyright © 2017年 davinci. All rights reserved.
//

#import "AddViewController.h"
#import "AddTableViewCell.h"
#import "AddHeaderView.h"
#import "AddOptional.h"
#import "AddSearchModel.h"

@interface AddViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, weak)UIImageView * backImageView;

@property (nonatomic, strong)UITableView * tableView;

// 是否在搜索状态
@property (nonatomic, assign) BOOL isSearchState;

@property (nonatomic, strong)NSArray<AddOptional *>* productListArray;
@property (nonatomic, strong)NSArray<AddSearchModel *>* searchListArray;


@end

@implementation AddViewController


- (void)setProductListArray:(NSArray<AddOptional *> *)productListArray{
    
    _productListArray = productListArray;
    
    [self.tableView reloadData];
}


- (void)setSearchListArray:(NSArray<AddSearchModel *> *)searchListArray{
    
    _searchListArray = searchListArray;
    
    [self.tableView reloadData];
}

- (void)viewWillAppear:(BOOL)animated{
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
}


- (void)viewDidAppear:(BOOL)animated{
    
    [MBProgressHUD hideHUDForView:self.view animated:YES];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [AddOptional addOptionalWithSucess:^(NSArray<AddOptional *> *array) {
        
        self.productListArray = array;
        
    } error:^{
        NSLog(@"productList error");
    }];
    
    [self setupNavBar];
    [self setupUI];
}

- (void)setupNavBar{
    
    self.title = @"添加交易品种";
    
    UIButton * backButton = [[UIButton alloc] init];
    
    backButton.frame = CGRectMake(15 * SCREENH6, 5 * SCREENH6, 12 * SCREENH6, 20 * SCREENH6);
    [backButton setImage:[UIImage imageNamed:@"1返回"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backToAccountView) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    self.navigationItem.leftBarButtonItem = leftButton;
}



- (void)setupUI{
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIImageView * backImage = [[UIImageView alloc] init];
    
    backImage.image = [UIImage imageNamed:@"背景"];
    
    self.backImageView = backImage;
    
    [self.view addSubview:self.backImageView];
    
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.tableView registerClass:[AddHeaderView class] forHeaderFooterViewReuseIdentifier:@"addHeader"];
    self.tableView.sectionHeaderHeight = 35 * SCREENH6;
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.bounces = NO;
    
    [self.tableView registerClass:[AddTableViewCell class] forCellReuseIdentifier:@"addCell"];
    [self.view addSubview:self.tableView];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange:) name:UITextFieldTextDidChangeNotification object:nil];
}


- (void)viewDidLayoutSubviews{
    
    [self.backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.top.bottom.mas_equalTo(self.view);
    }];
    
}


// 监听方法
- (void)textChange:(NSNotification *)note{
    
    UITextField * textField = (UITextField *)[note object];
    
    // 如果项目大, 搜索的地方多 , 就要使用多线程, 然后在刷新tableView 的时候 回到主线程
    [self startSearch:textField.text];
}


 //开始所搜
- (void)startSearch:(NSString *)string{
    
    
    if (![string isEqualToString:@""] && ![string isEqual:[NSNull null]]) {
    
        [AddSearchModel with:string addSearchWithSucess:^(NSArray<AddSearchModel *> *array) {
            
            self.searchListArray = array;
            
        } error:^{
            
        }];
    
        self.isSearchState = YES;
    }
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (_isSearchState) {
        
        return self.searchListArray.count;
    }
    
    return self.productListArray.count;
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        
        AddTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"addCell" forIndexPath:indexPath];
        

//        cell.showImage.image = [UIImage imageNamed:@"添加"];
    
//    UserDTO * dto = nil;
    
    if (_isSearchState) {
        
        AddSearchModel * model = self.searchListArray[indexPath.row];
        
        cell.searchModel = model;
    }else{
        
        AddOptional * model = self.productListArray[indexPath.row];
        
        cell.model = model;
    }
    
        return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 70 * SCREENH6;
}




- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    AddHeaderView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"addHeader"];
    //    header.backgroundColor = [UIColor blackColor];
    //    header.contentView.backgroundColor = [UIColor whiteColor];
    //    header.contentView.alpha = 0.1;
    return header;
}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    AddTableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
    
//    NSLog(@"%@",cell.englishLabel.text);
    
    NSDictionary * parameters = @{@"symbolId":cell.englishLabel.text};
    
    [[NetworkTool sharedTools] POST:@"symbols/optional" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {
        
        cell.showImage.image = [UIImage imageNamed:@"对勾"];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (void)backToAccountView{
    
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark - 懒加载

//- (NSMutableArray *)storeUserDTOList{
//    if (_storeUserDTOList == nil) {
//        _storeUserDTOList = [[NSMutableArray alloc] init];
//        [_storeUserDTOList addObjectsFromArray:self.productListArray];//self.productListArray;
//    }
//    return _storeUserDTOList;
//}
//
//- (NSMutableArray *)searchUserDTOList{
//    if (_searchUserDTOList == nil) {
//        _searchUserDTOList = [[NSMutableArray alloc] init];
//    }
//    return _searchUserDTOList;
//}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
